<?php
class Page extends SiteTree {

    private static $db = array(
    );

    private static $has_one = array(
        //'ECategory' => 'ECategory'
    );

    public function getCMSFields(){
        $field = parent::getCMSFields();

//        $arrCategory = ECategory::get()->map('Title', 'Title')->toArray();
//        asort($arrCategory);
//        $categoryField = new DropdownField('ECategoryID', 'Select Category',
//            $arrCategory
//        );
//        $categoryField->setEmptyString('Select one');
//        $field->findOrMakeTab('Root.Category','Category');
//        $field->addFieldsToTab('Root.Category',array(
//            $categoryField
//        ));

        return $field;
    }
}
class Page_Controller extends ContentController {

    /**
     * An array of actions that can be accessed via a request. Each array element should be an action name, and the
     * permissions or conditions required to allow the user to access it.
     *
     * <code>
     * array (
     *     'action', // anyone can access this action
     *     'action' => true, // same as above
     *     'action' => 'ADMIN', // you must have ADMIN permissions to access this action
     *     'action' => '->checkAction' // you can only access this action if $this->checkAction() returns true
     * );
     * </code>
     *
     * @var array
     */
    private static $allowed_actions = array (
        'FormSearch'
    );


    public function init() {
        parent::init();
        // You can include any CSS or JS required by your project here.
        // See: http://doc.silverstripe.org/framework/en/reference/requirements
    }

    function getRootPage($page = false) {
        $page = $page ? $page : $this;
        if ($page->ParentID != 0) {
            return $this->getRootPage($page->Parent());
        } else
            return $page;
    }

    public function FormSearch(){
        $fields = new FieldList( array(new TextField('TextSearch')));
        $actions = new FieldList(new FormAction('doSearch','submit'));
        $form = new Form($this, 'FormSearch',$fields,$actions);
        return $form;
    }

    public function doSearch($data, $form)
    {
        $products = EProduct::get()->where(array("\"Title\" LIKE '%".$data['TextSearch']."%'" or "\"Content\" LIKE '%".$data['TextSearch']."%'"));
        print_r("<ul>");
        foreach($products as $product) {
            print_r("<li><a href='".$product->UrlSegment."'>".$product->Title."</a></li>");
        };
        print_r("<ul>");
        return $this->customise(array("FormSearch"=>$this->FormSearch()))->renderWith(array('SearchPage','Page'));
    }

    public function result(){

    }

    public function checkLink(){
        return EpalShop_Controller::$URLCategory.'/'.$this->UrlSegment;
    }

    public function getAllCategory(){
        return DataObject::get('ECategory');
    }

}
